package com.zhoug.plugin.android.constgenerator.dialog;

import com.zhoug.plugin.android.constgenerator.beans.ConstField;

import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;

public class JsonDialog extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JTextArea textArea1;
    private JTextField textField1;
    private OnListener onListener;

    public JsonDialog() {
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);

        textField1.setText(ConstField.getDefaultFieldName());
        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
                List<ConstField> fields = new ArrayList<>();
                String text = textArea1.getText();
                System.out.println("input:" + text);
                if (text != null && text.length() > 0) {
//                    0：其它,1：食品使用,2：食品流通,3：食品生产，4：药品使用，5：药品流通，6：药品生产
                    text = text.trim().replace(";", ",")
                            .replace("{", "")
                            .replace("}", "")
                            .replace("{", "")
                            .replace("}", "")
                            .replace("；", ",")
                            .replace("：", ":")
                            .replace("，", ",");
                    String suffix = textField1.getText().trim();
                    if(suffix.trim().length()>0){
                        ConstField.setDefaultFieldName(suffix);
                    }
                    String[] split = text.split(",");
                    for (int i = 0; i < split.length; i++) {
                        //0：其它
                        try {
                            ConstField constField = new ConstField();
                            String s = split[i];
                            System.out.println(s);
                            String[] vl = s.split(":");
                            String value = vl[0].trim();
                            String name = vl[1].trim();
                            constField.setValue(Integer.parseInt(value));
                            constField.setName(name);
                            constField.setFieldName(suffix+"_"+value);
                            fields.add(constField);
                        } catch (NumberFormatException numberFormatException) {
                            numberFormatException.printStackTrace();
                        }
                    }
                    System.out.println("fields:" + fields);
                }

                onListener.onOk(fields);

            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

        // call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }

    private void onOK() {
        // add your code here
        dispose();
    }

    private void onCancel() {
        // add your code here if necessary
        dispose();
    }

    public static void main(String[] args) {
        JsonDialog dialog = new JsonDialog();
        dialog.pack();
        dialog.setVisible(true);
        System.exit(0);
    }

    public void setOnListener(OnListener onListener) {
        this.onListener = onListener;
    }

    public interface OnListener {
        void onOk(List<ConstField> fields);

        void onCancel();
    }


}
